package cn.qj.ymcc.service.impl;

import cn.qj.ymcc.constants.BusinessConstants;
import cn.qj.ymcc.domain.MediaFile;
import cn.qj.ymcc.service.IMediaSenderService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MediaSenderServiceImpl implements IMediaSenderService {
    /**
     * 发送异步推流消息
     *
     * @param mediaFile
     * @return
     */
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public Boolean send(MediaFile mediaFile) {
        if (mediaFile == null) {
            throw new RuntimeException("消息体没有数据");
        }
        SendResult result = rocketMQTemplate.syncSend(
                BusinessConstants.ROCKETMQ_TOPIC_MEDIA+":"+BusinessConstants.ROCKETMQ_TAGS_MEDIA,
                MessageBuilder.withPayload(JSONObject.toJSONString(mediaFile)).build());
//打印结果
        System.out.println(result);
        //校验发送结果
        log.info("发送结果:{}",result);
        return result.getSendStatus() == SendStatus.SEND_OK;
    }
}
